package com.example.web;

import com.example.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    //同步阻塞的Handler
    @GetMapping("/sync")
    public User getUserSync(){
        log.info("同步开始执行");
        User user=getUser();
        log.info("同步执行完毕");
        return user;
    }

    private User getUser() {
        //模拟业务
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return User.of("rose",22);
    }
    //异步的handler
    @GetMapping("mono")
    public Mono<User> getUserMono(){
        log.info("异步开始执行");
        Mono<User> userMono = Mono.fromSupplier(this::getUser);
        log.info("异步执行完毕");
        return userMono;

    }
    @GetMapping("flux")
    public Flux<User> getUserFlux(){
        log.info("flux开始执行");
        Flux<User> flux=getUserWithFlux();

        System.out.println("flux执行完毕");
        return flux;
    }

    private Flux<User> getUserWithFlux() {
        return Flux.interval(Duration.ofSeconds(1))//每隔一秒发射一个元素
                //取出前五个并转换成user对象
                .take(5).map(i-> User.of("user_"+i,20+i.intValue()));

    }

    public static void main(String[] args) {
        // 生成包含两个字符串的Flux："Hello", "World"
        Flux.just("Hello","World").subscribe(System.out::println);
        //// 从一个Integer数组中创建Flux
        Flux.fromArray(new Integer[]{1,2,3}).subscribe(System.out::println);
        //空的Flux
        Flux.empty().subscribe(System.out::println);
        //创建一个包含1到10的整数integer类型的flux
        Flux.range(1,10).subscribe(System.out::println);
        //每隔一秒生一个元素
        Flux.interval(Duration.ofSeconds(1)).subscribe(System.out::println);
        Flux.generate(sink->{
            sink.next("hello");
            sink.complete();
        }).subscribe(System.out::println);
//generate方法
      final   Random random = new Random();
      Flux.generate(ArrayList::new,(list,sink)->{
          int i = random.nextInt(100);
          list.add(i);
          sink.next(i);
          if (list.size()==10){
              sink.complete();
          }
          return list;
      }).subscribe(System.out::println);

      //create方法
        Flux.create(sink->{
            for (int i = 0; i < 10; i++) {
                sink.next(i);
            }
            //结束循环
            sink.complete();
        }).subscribe(System.out::println);


        Mono<String> hello = Mono.just("hello");
        Mono<Object> hello1 = Mono.create(monoSink -> monoSink.success("hello"));
        Mono<String> stringMono = Mono.fromSupplier(() -> "hello");
        Mono<String> stringMono1 = Mono.fromFuture(CompletableFuture.supplyAsync(() -> "hello"));
    }
}
